var first = true;
$(function() {
	initOrgGrid();
	initOrgTree();
});

function initOrgTree() {
	var url = '../../orgController.action?method=orgTreeNodes';
	$("#orgTree").tree({
		url : url,
		lines : true,
		onBeforeExpand : function(node, param) {
			$('#orgTree').tree('options').url = url + "&id=" + node.id;
		},
		loadFilter : function(data) {
			if (data.nodes) {
				return data.nodes;
			} else {
				return data;
			}
		},
		onClick : function(node) {
			orgTreeNodeClick(node.id);
		},
		onLoadSuccess : function(node, data) {
			var root = $('#orgTree').tree('getRoot');
			if (root != null) {
				$('#orgTree').tree('expand', root.target);
				if (first) {
					orgTreeNodeClick(root.id);
					first = false;
				}
			}
		}
	});
}

function initOrgGrid() {
	var url = "../../orgController.action?method=pageOrg";
	$('#orgGrid').datagrid(
			{
				url : url,
				queryParams : {
					id : '',
					keyWord : ''
				},
				columns : [ [
						{
							title : '部门名称',
							field : 'name'
						},
						{
							title : '联系人',
							field : 'contact'
						},
						{
							title : '电话',
							field : 'phone'
						},
						{
							title : '地址',
							field : 'address'
						},
						{
							title : '备注',
							field : 'remark'
						},
						{
							title : '排序',
							field : 'sort'
						},
						{
							title : "操作",
							align : 'center',
							field : 'id',
							formatter : function(value, row, index) {
								var html = "<a href='#' onclick=\"modOrg('"
										+ value + "');\">修改</a>&nbsp;";
								html += "<a href='#' onclick=\"delOrg('"
										+ value + "');\">删除</a>";
								return html;
							}
						} ] ],
				method : 'post',
				striped : true,
				fitColumns : true,
				singleSelect : false,
				pagination : true,
				nowrap : false,
				showFooter : true
			});
}

function orgTreeNodeClick(id) {
	var keyWord = '';

	var queryParams = $('#orgGrid').datagrid('options').queryParams;
	queryParams.id = id;
	queryParams.keyWord = keyWord;

	$('#orgGrid').datagrid('load');
}

function delOrg(id) {
	var url = "../../orgController.action?method=delOrg";
	$.ajax({
		type : "post",
		url : url,
		data : {
			id : id
		},
		dataType : "json",
		success : function(data) {
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		}
	});
}

function modOrg(id) {

}